#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using std::max;
using std::vector;
using std::unordered_map;
using std::unordered_set;


class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int, int> mp;
        int ans = 0;
        for (auto n: nums) {
            if (mp.find(n) == mp.end()) {
                int left = (mp.find(n-1) == mp.end()) ? 0 : mp[n-1];
                int right = (mp.find(n+1) == mp.end()) ? 0 : mp[n+1];
                mp[n] = 1 + left + right;
                ans = max(ans, mp[n]);
                mp[n] = -1;
                mp[n-left] = 1 + left + right;
                mp[n+right] = 1 + left + right;
                
                
            }
        }
        return ans;
    }
};